package com.time.company.components.task;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.time.company.R;
import com.time.company.base.BaseRecAdapter;
import com.time.company.base.BaseViewHolder;
import com.time.company.servermodel.task.TaskData;
import com.time.company.utils.TimeUtils;

import java.util.List;

/**
 * 任务 - 时间打卡
 * Created by Bing on 2017/11/9.
 * E-mail is tx16bing@163.com
 */

public class ClockAdapter extends BaseRecAdapter<TaskData.ClockInArray> {

    private String mNowTime;

    public void setNowTime(String nowTime) {
        this.mNowTime = nowTime;
    }

    public ClockAdapter(Context mContext, List<TaskData.ClockInArray> list, int viewTypeCount) {
        super(mContext, list, viewTypeCount);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = inflater.inflate(R.layout.item_task_clock, parent, false);
        ClockViewHolder clockViewHolder = new ClockViewHolder(convertView);
        convertView.setTag(clockViewHolder);
        return clockViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        final TaskData.ClockInArray clock = getItem(position);
        ClockViewHolder clockHolder = (ClockViewHolder) holder;
        clockHolder.tvTitle.setText(clock.getTitle());

        if (position == mList.size() - 1) {
            clockHolder.divider.setVisibility(View.GONE);
        } else {
            clockHolder.divider.setVisibility(View.VISIBLE);
        }

        if (TextUtils.equals(mContext.getString(R.string.dis_recruiting), clock.getState())) {

            if (TextUtils.equals(clock.getType(), "unStartAct")) {

                // 征集中，未开始
                clockHolder.tvClock.setText("即将开始");
                clockHolder.tvClock.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                clockHolder.tvClock.setBackgroundResource(R.drawable.selector_white_xxx_bg);
                clockHolder.tvTime.setVisibility(View.VISIBLE);
                clockHolder.tvTime.setText(new StringBuilder()
                        .append("活动时间：")
                        .append(clock.getStartDate())
                        .append("至")
                        .append(clock.getEndDate()));
                clockHolder.tvDown.setVisibility(View.VISIBLE);
                boolean canGo = TimeUtils.compareTime(mNowTime, clock.getStartTime());
                if (canGo) {
                    long[] distanceTimes = TimeUtils.getDistanceTimes(mNowTime, clock.getStartTime());
                    if (distanceTimes.length > 0) {
                        long day = distanceTimes[0];
                        clockHolder.tvDown.setText(new StringBuilder().append("倒计时").append(day).append("天"));
                    } else {
                        clockHolder.tvDown.setVisibility(View.GONE);
                    }
                } else {
                    clockHolder.tvDown.setVisibility(View.GONE);
                }
            } else if (TextUtils.equals(clock.getType(), "canStartAct")) {
                // 征集中，开始活动
                clockHolder.tvClock.setBackgroundResource(R.drawable.selector_confirm_second);
                clockHolder.tvDown.setVisibility(View.VISIBLE);
                clockHolder.tvDown.setText("更改地点");
                clockHolder.tvDown.setTextColor(ContextCompat.getColor(mContext, R.color.colorThirdPrimary));
                clockHolder.tvDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 重新选择地点
                        if (null != onItemClickListener) {
                            if (TextUtils.isEmpty(clock.getId())) {
                                return;
                            } else {
                                onItemClickListener.onItemClick(v, position, clock);
                            }
                        }
                    }
                });

                clockHolder.tvClock.setText("开始活动");
                clockHolder.tvClock.setTextColor(ContextCompat.getColor(mContext, R.color.black_x));
                clockHolder.tvTime.setVisibility(View.VISIBLE);
                clockHolder.tvTime.setText(new StringBuilder().append("位置：").append(clock.getLocation()));
            }

        } else if (TextUtils.equals(mContext.getString(R.string.dis_executing), clock.getState())) {
            // 执行中 结束活动
            if (TextUtils.equals("canEndAct", clock.getType())) {

                clockHolder.tvClock.setBackgroundResource(R.drawable.selector_confirm_second);
                clockHolder.tvDown.setVisibility(View.VISIBLE);
                clockHolder.tvDown.setText("更改地点");
                clockHolder.tvDown.setTextColor(ContextCompat.getColor(mContext, R.color.colorThirdPrimary));
                clockHolder.tvDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 重新选择地点
                        if (null != onItemClickListener) {
                            if (TextUtils.isEmpty(clock.getId())) {
                                return;
                            } else {
                                onItemClickListener.onItemClick(v, position, clock);
                            }
                        }
                    }
                });

                clockHolder.tvClock.setText("结束活动");
                clockHolder.tvClock.setTextColor(ContextCompat.getColor(mContext, R.color.black_x));
                clockHolder.tvTime.setVisibility(View.VISIBLE);
                clockHolder.tvTime.setText(new StringBuilder().append("位置：").append(clock.getLocation()));
            }

        } else if (TextUtils.equals(mContext.getString(R.string.dis_finished), clock.getState())) {
            clockHolder.tvClock.setText("已结束");
            clockHolder.tvClock.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            clockHolder.tvClock.setBackgroundResource(R.drawable.selector_white_xxx_bg);
            clockHolder.tvDown.setVisibility(View.GONE);
            clockHolder.tvTime.setVisibility(View.INVISIBLE);
        }

        clockHolder.tvClock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener) {
                    if (TextUtils.isEmpty(clock.getId())) {
                        return;
                    } else {
                        onItemClickListener.onItemClick(v, position, clock);
                    }
                }
            }
        });

        clockHolder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener) {
                    if (TextUtils.isEmpty(clock.getId())) {
                        return;
                    } else {
                        onItemClickListener.onItemClick(v, position, clock);
                    }
                }
            }
        });

        if (position == getItemCount() - 1) {
            clockHolder.divider.setVisibility(View.GONE);
        } else {
            clockHolder.divider.setVisibility(View.VISIBLE);
        }

    }

    class ClockViewHolder extends BaseViewHolder {

        public LinearLayout llParent;
        public TextView tvTitle;
        public TextView tvTime;
        public TextView tvClock;
        public TextView tvDown;
        public View divider;

        public ClockViewHolder(View itemView) {
            super(itemView);
            llParent = getView(R.id.ll_clock_item_parent);
            tvTitle = getView(R.id.tv_clock_title);
            tvTime = getView(R.id.tv_clock_time);
            tvClock = getView(R.id.tv_clock_button);
            tvDown = getView(R.id.tv_clock_down);
            divider = getView(R.id.v_divider);
        }
    }

}
